//
//  successDemo.m
//  AoJi
//
//  Created by chen on 15/2/12.
//  Copyright (c) 2015年 xuanheng. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "aojiUSA.h"
#import "PPiFlatSegmentedControl.h"
#import "MJRefresh.h"
#import "AppDelegate.h"
#import "DataSingleTon.h"
typedef NS_ENUM(NSInteger,LoadingType)
{
    LoadingDefault,
    LoadingRefreshHeader,
    LoadingRefreshFooter,
    
    
};

@interface aojiUSA()
@end

@implementation aojiUSA
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        goldenIndex= 1;
        [self gotcitylist];
        successArray = [NSMutableArray array];
        viewerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width,frame.size.height) style:UITableViewStylePlain];
        viewerTableView.delegate = self;
        viewerTableView.dataSource = self;
        viewerTableView.backgroundColor = [self getCorlor:@"E6E4E4"];
        [self addSubview:viewerTableView];
        
        
        
//        [viewerTableView addHeaderWithCallback:^{
//          
//            [self runsuccessdemo:LoadingRefreshHeader];
//        }];
//        
//        [viewerTableView addFooterWithCallback:^{
//            [self runsuccessdemo:LoadingRefreshFooter];
//           
//        }];
        [self runsuccessdemo:LoadingDefault];
        
    }
    return self;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [successArray count]+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.backgroundColor = [self getCorlor:@"E6E4E4"];
    if (indexPath.row == [successArray count]) {
        UILabel* jingqingQiDai = [[UILabel alloc] initWithFrame:CGRectMake(125, 10, 150, 40)];
//        jingqingQiDai.text = @"敬请期待!";
        [cell addSubview:jingqingQiDai];

        return cell;
    }
    UIView* cellBackground = [[UIView alloc] initWithFrame:CGRectMake(4, 5, 313, 255)];
    cellBackground.backgroundColor = [UIColor whiteColor];
    [cell addSubview:cellBackground];
    
    NSMutableDictionary *tempDic = successArray[indexPath.row];
        //人物图片
        UIImageView* imgShow = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 313, 200)];
    NSString* finalUrl = [NSString stringWithFormat:@"%@%@",baseUrl,[tempDic objectForKey:@"img"]];

        [imgShow sd_setImageWithURL:[NSURL URLWithString:finalUrl] placeholderImage:nil];
        [cellBackground addSubview: imgShow];
    //介绍
    UILabel* contentlab = [[UILabel alloc] initWithFrame:CGRectMake(10,200, 300, 50)];
    contentlab.text = [tempDic objectForKey:@"content"];
    contentlab.font = [UIFont systemFontOfSize:13];
    contentlab.textColor = [self getCorlor:@"504F4F"];
    contentlab.numberOfLines = 0;
    contentlab.textColor = [UIColor colorWithRed:52/255 green:52/255 blue:52/255 alpha:1.0];
    [cellBackground addSubview:contentlab];
    

    return cell;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 32.0;
//}
//
//
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    UIView* tempview = [[UIView alloc] init];
//    tempview.backgroundColor = [UIColor blackColor];
//    return tempview;
//}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == [successArray count]) {
        return 80;
    }
    else{
        return 260;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
        {
            NSLog(@"学校详情");
        }
            break;
        case 1:
        {
            NSLog(@"活动详情");
        }
            break;
        case 2:
        {
            NSLog(@"面试详情");
        }
            break;
        case 3:
        {
            NSLog(@"路线详情");
        }
            break;
        default:
        {
            NSLog(@"课程详情");
        }
            break;
    }
}

-(void)runsuccessdemo:(LoadingType)loadtype
{
    NSMutableDictionary* dicSave = [[NSMutableDictionary alloc] init];
    [dicSave setObject:[DataSingleTon shareDataSingleTon].catdir  forKey:@"country"];
    if (loadtype==LoadingRefreshFooter) {
//        dicSave setObject:<#(id)#> forKey:@"lastid"
    }
    
    [YHNetwork getInformationWithReguestType:RequestTypeGet WithParameters:dicSave WithApi:@"study/advantage" WhenSuccess:^(NSMutableDictionary *response) {
        for (id dic in response) {
            [successArray addObject:dic];
        }
        [viewerTableView reloadData];
        goldenIndex++;
//        [viewerTableView footerEndRefreshing];

    } WhenFailure:^(NSError *failure) {
        
    }];
}


-(void)gotcitylist{
    NSMutableDictionary* dicSave = [[NSMutableDictionary alloc] init];
    [YHNetwork getInformationWithReguestType:RequestTypeGet WithParameters:dicSave WithApi:@"city" WhenSuccess:^(NSMutableDictionary *response) {
        for (id dic in response) {
            [cityList addObject:dic];
        }
    } WhenFailure:^(NSError *failure) {
        
    }];
}


-(void)useTapGestureRecognizerclose {
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletapPressGesture:)];
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    [app.window addGestureRecognizer:tapGesture];

}
//
-(void)handletapPressGesture:(UITapGestureRecognizer*)sender{
    CGPoint point = [sender locationInView:self];
    UIView* babalView = (UIView*)[self viewWithTag:998];
    if (point.y<babalView.frame.origin.y || point.y >babalView.frame.origin.y+babalView.frame.size.height||point.x<babalView.frame.origin.x||point.x>babalView.frame.origin.x+babalView.frame.size.width) {
                                     //一定要移除手势 否则下次 没有子视图的时候 点击 会崩溃拉
//                                     [babalView removeFromSuperview];
        [phoneTextView resignFirstResponder];
        [nameTextView resignFirstResponder];
        [cityTextView resignFirstResponder];
        [emailTextView resignFirstResponder];
        [boardTextView resignFirstResponder];

    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[[event allTouches] anyObject];
    if (touch.tapCount >=1) {
        [phoneTextView resignFirstResponder];
        [nameTextView resignFirstResponder];
        [cityTextView resignFirstResponder];
        [emailTextView resignFirstResponder];
        [boardTextView resignFirstResponder];
    }
}

@end
